package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.entity.po.SysMaterialType;
import com.xinsoft.mapper.SysMaterialMapper;
import com.xinsoft.mapper.SysMaterialTypeMapper;
import com.xinsoft.service.SysMaterialTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author xuhb
* @description 针对表【sys_material_type(物料类型)】的数据库操作Service实现
* @createDate 2023-05-23 13:14:54
*/
@Service
public class SysMaterialTypeServiceImpl extends ServiceImpl<SysMaterialTypeMapper, SysMaterialType>
    implements SysMaterialTypeService{

    @Resource
    SysMaterialTypeMapper sysMaterialTypeMapper;

    @Resource
    SysMaterialMapper sysMaterialMapper;

    @Override
    public String saveMaterialType(SysMaterialType materialType) {
        LambdaQueryWrapper<SysMaterialType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMaterialType::getMtypeCode,materialType.getMtypeCode())
                .or().eq(SysMaterialType::getMtypeName,materialType.getMtypeName());
        List<SysMaterialType> typeList = sysMaterialTypeMapper.selectList(queryWrapper);
        if(typeList!=null&&typeList.size()>0){
            return "代码与名称均不可以重复";
        }else{
            this.save(materialType);
            return "";
        }
    }

    @Override
    public String updateMaterialType(SysMaterialType materialType) {
        LambdaQueryWrapper<SysMaterialType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(materialType.getId()!=null,SysMaterialType::getId,materialType.getId());
        queryWrapper.and((wrapper)->{
                    wrapper.eq(SysMaterialType::getMtypeCode,materialType.getMtypeCode())
                            .or().eq(SysMaterialType::getMtypeName,materialType.getMtypeName());
                }
        );
        List<SysMaterialType> typeList = sysMaterialTypeMapper.selectList(queryWrapper);
        if(typeList!=null&&typeList.size()>0){
            return "代码与名称均不可以重复";
        }else{
            this.updateById(materialType);
            return "";
        }
    }

    @Override
    public String deleteMaterialTypeById(List<Integer> ids) {
        LambdaQueryWrapper<SysMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysMaterial::getMaterialTypeId,ids);
        List<SysMaterial> materialList = sysMaterialMapper.selectList(queryWrapper);
        if(materialList!=null&& materialList.size()>0){
            return "该物料类型下有子物料，不可删除";
        }else {
            this.removeByIds(ids);
            return "";
        }
    }

    @Override
    public List<SysMaterialType> listMtype(String mtypeName) {
        LambdaQueryWrapper<SysMaterialType> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(mtypeName)){
            queryWrapper.like(SysMaterialType::getMtypeName,mtypeName);
        }
        return sysMaterialTypeMapper.selectList(queryWrapper);
    }
}




